package com.maoye.ossp.biz.statement.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.maoye.ossp.biz.statement.entity.StatementSelfrun;
import com.maoye.ossp.biz.statement.entity.StatementSelfrunError;
import com.maoye.ossp.biz.statement.service.StatementSelfrunErrorService;
import com.maoye.ossp.biz.statement.service.StatementSelfrunService;
import com.maoye.ossp.biz.statement.util.BizStatementConstant;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.msg.entity.Message;
import com.maoye.ossp.sys.msg.entity.TodoMessage;
import com.maoye.ossp.sys.msg.handler.MsgStrategyContext;
/**
 * 自营对账异常单controller类
 * @author yanghao
 *
 */
@Controller
@RequestMapping(value = "biz/statement/statementSelfrunError/")
public class StatementSelfrunErrorController extends BaseController {
	
	@Resource
	private StatementSelfrunErrorService statementSelfrunErrorService;
	@Resource
	private StatementSelfrunService statementSelfrunService;
	@Resource
	MsgStrategyContext context;

	/**
	 * 填写异常单信息
	 * @param statementSelfrun
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "add")
	public String add(StatementSelfrun statementSelfrun ,HttpServletRequest request, Model model) {
		String id = statementSelfrun.getId().toString();
		StatementSelfrun selfrun = statementSelfrunService.get(id);
		StatementSelfrunError error = new StatementSelfrunError();
		error.setSaleDate(selfrun.getDate());
		error.setBillType(selfrun.getBillType());
		error.setSupplierName(selfrun.getSupplierName());
		error.setSupplierNo(selfrun.getSupplierNo());
		error.setSelfRunStatementId(selfrun.getId());
		model.addAttribute("statementSelfrun", selfrun);
		model.addAttribute("statementSelfrunError", error);
		return "/biz/statement/statementSelfrunError/add";
	}
	/**
	 * 查看异常单列表
	 * @param statementSelfrunError
	 * @param request
	 * @param model
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "list")
	public String list(StatementSelfrunError statementSelfrunError ,HttpServletRequest request, Model model) {
		Map map = new HashMap();
		String supplierNo = statementSelfrunError.getSupplierNo();
		Date createTime = statementSelfrunError.getCreateTime();
		Date saleDate = statementSelfrunError.getSaleDate();
		Integer isConfirmed = statementSelfrunError.getIsConfirmed();
		map.put("supplierNo", supplierNo);
		map.put("createTime", createTime);
		map.put("saleDate", saleDate);
		map.put("isConfirmed", isConfirmed);
		List<StatementSelfrunError> list = statementSelfrunErrorService.getByTerm(map);
		model.addAttribute("list", list);
		return "/biz/statement/statementSelfrunError/list";
	}
	/**
	 * 保存异常单信息并发送通知到采购
	 * @param statementSelfrunError
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "save")
	public String save(StatementSelfrunError statementSelfrunError ,HttpServletRequest request, Model model)  {
		statementSelfrunError.setId(idGenerator.getUId());
		statementSelfrunError.setCreateTime(new Date());
		statementSelfrunError.setIsConfirmed(BizStatementConstant.UNCONFIRMED_SELFRUN);
		try {
			statementSelfrunErrorService.create(statementSelfrunError);
			//发送通知
			TodoMessage todo = new TodoMessage();
			context.send(todo);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}	
		return "redirect:/biz/statement/statementSelfrun/list";
	}
	/**
	 * 通知供应商异常不存在
	 * @param statementSelfrunError
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 * @author tf
	 * @data 2014年3月22日
	 */
	@RequestMapping(value = "exceptionError")
	public void exceptionError(StatementSelfrunError statementSelfrunError ,HttpServletRequest request, HttpServletResponse response,Model model) throws IOException  {
		StringBuilder jsonStr = new StringBuilder();
		response.setCharacterEncoding("utf-8");
		String contextPath=request.getContextPath();
		PrintWriter out = response.getWriter();
		try {
			StatementSelfrunError error = statementSelfrunErrorService.get(statementSelfrunError.getId().toString());
			//发送通知
			TodoMessage todo = new TodoMessage();
			context.send(todo);
			jsonStr.append("{success:true}");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			jsonStr.append("{success:false}");
		}	
		out.print(jsonStr.toString());
		out.flush();
		out.close();
	}
	/**
	 * 查看异常单详细信息
	 * @param statementSelfrunError
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "{id}/view")
	public String view(@PathVariable String id ,HttpServletRequest request, Model model) {
		StatementSelfrunError statementSelfrunError = statementSelfrunErrorService.get(id);
		model.addAttribute("statementSelfrunError", statementSelfrunError);
		return "/biz/statement/statementSelfrunError/view";
	}
	/**
	 * 填写异常处理方案
	 * @param statementSelfrunError
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "{id}/erroScheme")
	public String erroScheme(@PathVariable String id ,HttpServletRequest request, Model model) {
		StatementSelfrunError statementSelfrunError = statementSelfrunErrorService.get(id);
		model.addAttribute("statementSelfrunError", statementSelfrunError);
		return "/biz/statement/statementSelfrunError/erroScheme_add";
	}
	/**
	 * 保存异常处理方案
	 * @param statementSelfrunError
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/saveErroScheme")
	public String saveErroScheme(StatementSelfrunError statementSelfrunError,HttpServletRequest request, Model model) {
		statementSelfrunErrorService.update(statementSelfrunError);
		return "redirect:/biz/statement/statementSelfrun/list";
	}
	/**
	 * 保存异常处理方案并确认异常
	 * @param statementSelfrunError
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "{id}/saveErroScheme")
	public String saveErroScheme(@PathVariable String id ,StatementSelfrunError statementSelfrunError,HttpServletRequest request) {
		statementSelfrunError.setIsConfirmed(BizStatementConstant.ISCONFIRMED_SELFRUN);
		statementSelfrunErrorService.update(statementSelfrunError);
		return "redirect:/biz/statement/statementSelfrunError/list";
	}
	public StatementSelfrunErrorService getStatementSelfrunErrorService() {
		return statementSelfrunErrorService;
	}

	public void setStatementSelfrunErrorService(
			StatementSelfrunErrorService statementSelfrunErrorService) {
		this.statementSelfrunErrorService = statementSelfrunErrorService;
	}

	public StatementSelfrunService getStatementSelfrunService() {
		return statementSelfrunService;
	}

	public void setStatementSelfrunService(
			StatementSelfrunService statementSelfrunService) {
		this.statementSelfrunService = statementSelfrunService;
	}
}
